package com.tjec.project.businessWorkbench.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.tjec.common.constant.Constants;
import com.tjec.common.utils.StringUtils;
import com.tjec.project.businessWorkbench.dto.BmsAttachmentDto;
import com.tjec.project.businessWorkbench.dto.WholeDocQueryDto;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.framework.security.service.TokenService;
import com.tjec.project.businessWorkbench.domain.BmsProjectDoc;
import com.tjec.project.businessWorkbench.service.IBmsProjectDocService;
import com.tjec.project.businessWorkbench.vo.BmsProjectDocVo;
import com.tjec.project.businessWorkbench.vo.WholeDocQueryVo;
import com.tjec.project.system.domain.BmsAttachmentHistory;
import com.tjec.project.system.service.IBmsAttachmentHistoryService;
import com.tjec.project.system.service.IBmsAttachmentService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.poi.ExcelUtil;
import com.tjec.framework.web.page.TableDataInfo;

/**
 * 项目文档 Controller
 * 
 * @author pangyognfeng
 * @date 2020-04-27
 */
 @Api(value = "项目文档", tags = {"项目文档接口"})
@RestController
@RequestMapping("/businessWorkbench/doc")
public class BmsProjectDocController extends BaseController
{
    @Autowired
    private IBmsProjectDocService bmsProjectDocService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IBmsAttachmentHistoryService bmsAttachmentHistoryService;
    @Autowired
    private IBmsAttachmentService bmsAttachmentService;

    @Value("${sftp.http}")
    private String http;
    @Value("${sftp.ip}")
    private String ip;
    /**
     * 查询项目文档 列表
     */
    @ApiOperation("查询项目文档列表")
    //@PreAuthorize("@ss.hasPermi('businessWorkbench:doc:list')")
    @GetMapping("/list")
    public TableDataInfo list(BmsProjectDoc bmsProjectDoc)
    {
        bmsProjectDocService.selectBmsProjectDocList(bmsProjectDoc);
        startPage();
        List<BmsProjectDoc> list = bmsProjectDocService.selectAllProjectDocQueryList(bmsProjectDoc);
        return getDataTable(list);
    }
    
    /**
     * 查询项目文档 列表成果校审
     */
    @ApiOperation("查询项目文档列表")
    //@PreAuthorize("@ss.hasPermi('businessWorkbench:doc:list')")
    @GetMapping("/listResult")
    public TableDataInfo listResult(BmsProjectDoc bmsProjectDoc)
    {
        startPage();
        List<BmsProjectDoc> list = bmsProjectDocService.selectBmsProjectDocListByResult(bmsProjectDoc);
        return getDataTable(list);
    }
    
    
    
    
    /**
     * 查询项目文档历史列表
     */
    @ApiOperation("查询项目文档历史列表")
    @GetMapping(value = "/historyList")
    public TableDataInfo historyList(BmsAttachmentHistory bmsAttachmentHistory)
    {
        startPage();
        List<BmsAttachmentHistory> list = bmsAttachmentHistoryService.selectBmsAttachmentHistoryList(bmsAttachmentHistory);
        return getDataTable(list);
    }
    
    /**
     * 更新文件
     */
    @ApiOperation("更新文件")
    @GetMapping(value = "updateHistoryAttach/{oldAttachId}/{newAttachId}")
    public AjaxResult updateHistoryAttach(@PathVariable("oldAttachId") String oldAttachId,@PathVariable("newAttachId") String newAttachId)
    {
        return AjaxResult.success(bmsAttachmentService.updateBmsAttachment(oldAttachId,newAttachId));
    }

    /**
     * 导出项目文档 列表
     */
    @ApiOperation("导出项目文档列表")
    //@PreAuthorize("@ss.hasPermi('businessWorkbench:doc:export')")
    @Log(title = "项目文档 ", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BmsProjectDoc bmsProjectDoc)
    {
        bmsProjectDocService.selectBmsProjectDocList(bmsProjectDoc);
        List<BmsProjectDoc> list = bmsProjectDocService.selectAllProjectDocQueryList(bmsProjectDoc);
        ExcelUtil<BmsProjectDoc> util = new ExcelUtil<BmsProjectDoc>(BmsProjectDoc.class);
        return util.exportExcel(list, "doc");
    }

    /**
     * 获取项目文档 详细信息
     */
    @ApiOperation("获取项目文档详细信息")
    //@PreAuthorize("@ss.hasPermi('businessWorkbench:doc:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(bmsProjectDocService.selectBmsProjectDocByGuid(guid));
    }
    
    /**
     * 获取项目文档 详细信息
     */
    @ApiOperation("获取项目文档列表-资料归档使用")
    @GetMapping(value = "getDocByCatalogId/{catalogId}")
    public AjaxResult getDocByCatalogId(@PathVariable("catalogId") String catalogId)
    {
        return AjaxResult.success(bmsProjectDocService.getDocByCatalogId(catalogId));
    }

    /**
     * 新增项目文档 
     */
    @ApiOperation("新增项目文档")
   // @PreAuthorize("@ss.hasPermi('businessWorkbench:doc:add')")
    @Log(title = "项目文档 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmsProjectDoc bmsProjectDoc)
    {
    	bmsProjectDoc.setCreateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
        return toAjax(bmsProjectDocService.insertBmsProjectDoc(bmsProjectDoc));
    }

    /**
     * 修改项目文档 
     */
    @ApiOperation("修改项目文档")
    //@PreAuthorize("@ss.hasPermi('businessWorkbench:doc:edit')")
    @Log(title = "项目文档 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmsProjectDoc bmsProjectDoc)
    {
    	bmsProjectDoc.setUpdateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
        return toAjax(bmsProjectDocService.updateBmsProjectDoc(bmsProjectDoc));
    }

    /**
     * 删除项目文档 
     * @throws Exception 
     */
    @ApiOperation("删除项目文档")
    //@PreAuthorize("@ss.hasPermi('businessWorkbench:doc:remove')")
    @Log(title = "项目文档 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids) throws Exception
    {
        return toAjax(bmsProjectDocService.deleteBmsProjectDocByGuids(guids));
    }
    
    /**
     * 文件移动 
     * @throws Exception 
     */
    @ApiOperation("文件移动")
   // @PreAuthorize("@ss.hasPermi('businessWorkbench:doc:move')")
    @Log(title = "文件移动", businessType = BusinessType.UPDATE)
    @PostMapping("/move")
    public AjaxResult moveBmsProjectDoc(@RequestBody BmsProjectDocVo bmsProjectDocVo) throws Exception
    {
        return toAjax(bmsProjectDocService.moveBmsProjectDoc(bmsProjectDocVo.getCatalogId(), tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId(), bmsProjectDocVo.getGuids()));
    }
    
    /**
     * 查询项目文档 列表
     */
    @ApiOperation("项目文档查询")
    //@PreAuthorize("@ss.hasPermi('businessWorkbench:doc:list')")
    @GetMapping("/wholeDocQuery")
    public TableDataInfo wholeDocQuery(WholeDocQueryVo wholeDocQueryVo)
    {
        startPage();
        if(wholeDocQueryVo!=null && wholeDocQueryVo.getCodes()!=null) {
        	List<String> resultList= new ArrayList<>(Arrays.asList(wholeDocQueryVo.getCodes().split(" ")));
        	wholeDocQueryVo.setCode(resultList);
        }
        List<WholeDocQueryDto> list = bmsProjectDocService.wholeDocQuery(wholeDocQueryVo);
        return getDataTable(list);
    }
    
    /**
     * 查询项目文档 列表
     */
    @ApiOperation("项目文档查询导出")
    //@PreAuthorize("@ss.hasPermi('businessWorkbench:doc:list')")
    @GetMapping("/wholeDocQueryExport")
    public AjaxResult wholeDocQuerExport(WholeDocQueryVo wholeDocQueryVo)
    {
         List<WholeDocQueryDto> list = bmsProjectDocService.wholeDocQuery(wholeDocQueryVo);
         ExcelUtil<WholeDocQueryDto> util = new ExcelUtil<WholeDocQueryDto>(WholeDocQueryDto.class);
         return util.exportExcel(list, "文档查询");
    }
}
